import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const Layout = (resolve) => {
  import('@/views/Layout').then((module) => {
    resolve(module)
  })
}

const IndexView = (resolve) => {
  import('@/views/IndexView').then((module) => {
    resolve(module)
  })
}

const CourseDetailView = (resolve) => {
  import('@/views/CourseDetailView').then((module) => {
    resolve(module)
  })
}

const SearchView = (resolve) => {
  import('@/views/SearchView').then((module) => {
    resolve(module)
  })
}


// const topSearch = (resolve) => {
//   import('@/components/topSearch').then((module) => {
//     resolve(module)
//   })
// }

// const banner = (resolve) => {
//   import('@/views/banner').then((module) => {
//     resolve(module)
//   })
// }

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/index/',
    },
    {
      path:'/index',
      component:Layout,
      children:[
        {
          path: '/',
          meta:{
            //keepAlive:true
          },
          component: IndexView
        }
      ]
    },
    {
      path:'/coursedetail/:id',
      component:Layout,
      children:[
        {
          path: '/',
          meta:{
            //keepAlive:true
          },
          component: CourseDetailView
        }
      ]
    },
    {
      path:'/search',
      component:SearchView
    },
  ]
})

router.beforeEach((to, from, next) => {
  //store.commit('updateLoadingStatus', {isLoading: true})  //页面跳转菊花图
    // if (to.matched.some(r=>r.meta.requireAuth)) {  // 判断该路由是否需要登录权限
    //     //store.dispatch('hasToken')

    //     if (store.state.user.username){
    //         next();
    //     }
    //     else {
    //         next({
    //             path: '/login',
    //             query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
    //         })
    //     }
    // }
    // else {
    //     next();
    // }
    if (to.matched.length === 0) { //如果未匹配到路由
      from.name ? next({ name: from.name }) : next('/'); //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
    } else {
      next(); //如果匹配到正确跳转
    }
})

router.afterEach(function (to) {
  //store.commit('updateLoadingStatus', {isLoading: false})
})

export default router;